﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Data.EntityClient;
using System.Threading;

namespace FredBot
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        System.Windows.Forms.NotifyIcon ni;
        Basic ircbot;

        public MainWindow()
        {
            InitializeComponent();

            ni = new System.Windows.Forms.NotifyIcon();
            ni.Icon = new System.Drawing.Icon("Main.ico");
            ni.Visible = true;
            ni.DoubleClick +=
                delegate(object sender, EventArgs args)
                {
                    this.Show();
                    this.WindowState = WindowState.Normal;
                };
            ni.ContextMenuStrip = new ContextMenuStrip();
            ni.ContextMenuStrip.Items.Add("Reconnect");
            ni.ContextMenuStrip.Items[0].Click += OnReconnectClick;
            ni.ContextMenuStrip.Items.Add("Quit");
            ni.ContextMenuStrip.Items[1].Click += OnQuitClick;

            this.WindowState = WindowState.Minimized;
            this.Hide();

            Thread thr = new Thread(new ThreadStart(LogReceiver.Parse));
            thr.IsBackground = true;
            thr.Start();

            ircbot = new Basic();
            ircbot.Start();
        }

        protected override void OnStateChanged(EventArgs e)
        {
            if (WindowState == WindowState.Minimized)
                this.Hide();

            base.OnStateChanged(e);
        }

        public void OnQuitClick(object sender, EventArgs e)
        {
            this.Close();
        }

        protected override void OnClosed(EventArgs e)
        {
            ni.Visible = false;
            ircbot.Stop();
            base.OnClosed(e);
        }

        public void OnReconnectClick(object sender, EventArgs e)
        {
            ircbot.Restart();
        }
    }
}
